package com.woniuxy.yygh.hsop.receiver;

import com.rabbitmq.client.Channel;
import com.woniuxy.common.rabbit.constant.MqConst;
import com.woniuxy.common.rabbit.service.RabbitService;
import com.woniuxy.yygh.hsop.service.ScheduleService;
import com.woniuxy.yygh.model.hosp.Schedule;
import com.woniuxy.yygh.vo.msm.MsmVo;
import com.woniuxy.yygh.vo.order.OrderMqVo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO: mq监听器 监听排班信息的service
 * <P> Date: 2022/3/6:11:46
 * <P> Description:
 */
@Component
public class HospitalReceiver {

    @Resource
    private ScheduleService scheduleService;

    @Resource
    private RabbitService rabbitService;

    /**
     * TODO 绑定对应的队列 交换机 路由名
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ORDER, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_ORDER),
            key = {MqConst.ROUTING_ORDER}
    ))

    public void receiver(OrderMqVo orderMqVo, Message message, Channel channel) throws IOException {
        //下单成功 得到内容 更新预约数
        Schedule schedule = scheduleService
                .getScheduleById(orderMqVo.getScheduleId());

        schedule.setReservedNumber(orderMqVo.getReservedNumber());
        schedule.setAvailableNumber(orderMqVo.getAvailableNumber());

        scheduleService.update(schedule);

        //发送短信
        MsmVo msmVo = orderMqVo.getMsmVo();

        if (null != msmVo) {
            rabbitService.sendMessage(
                    MqConst.EXCHANGE_DIRECT_MSM,
                    MqConst.ROUTING_MSM_ITEM,
                    msmVo)
            ;
        }
    }
}
